import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import './assets/css/global.css'
import './assets/fonts/iconfont.css'
import axios from 'axios'
import TreeTable from 'vue-table-with-tree-grid'
// 导入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
import echarts from 'echarts'
// require styles 导入富文本编辑器对应的样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import './assets/myicon/menus/iconfont.css'
// 时间过滤器
import moment from 'moment'

// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.prototype.$echarts = echarts

Vue.use(ElementUI)
axios.defaults.baseURL = '/account'
Vue.prototype.$http = axios
// token
axios.interceptors.request.use(config => {
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})
axios.interceptors.response.use(config => {
  NProgress.done()
  return config
})

Vue.config.productionTip = false

Vue.component('tree-table', TreeTable)
// 将富文本编辑器，注册为全局可用的组件
Vue.use(VueQuillEditor)

// 时间过滤器
Vue.filter('dateFormat', function(originVal) {
  const dt = new Date(originVal)
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDay() + '').padStart(2, '0')
  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

// 时间过滤器
Vue.filter('moment', function(value, formatString) {
  return moment(value).format(formatString)
})

// 挂载(这里我匹配了日期格式,这样的话能提高代码复用性)
Vue.prototype.$timeFormat = function(value) {
  // 匹配日期格式：yyyy-MM-dd
  const timeRegex = /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))$/
  // 匹配日期格式: yyyy-MM-dd HH:mm:ss
  const timeRegex1 = /^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/
  const flag1 = timeRegex.test(value)
  const flag2 = timeRegex1.test(value)
  let formatString = ''
  if (flag1 === true && flag2 === false) {
    formatString = 'yyyy-MM-dd'
  } else if (flag1 === false && flag2 === true) {
    formatString = 'yyyy-MM-dd HH:mm:ss'
  } else {
    formatString = 'yyyy-MM-dd HH:mm:ss'
  }
  return moment(value).format(formatString)
}

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
